
package net.sourceforge.jasonide.wizards;

import net.sourceforge.jasonide.core.MAS2JHandler;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.*;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

/**
 * The "New" wizard page allows setting the container for the new file as well as the file name. The page will only
 * accept file name without the extension OR with the extension that matches the expected one (asl).
 */
public class NewAgentWizardPage extends WizardNewFileCreationPage {
	
	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public NewAgentWizardPage(ISelection selection) {
		super("New AgentSpeak File", StructuredSelection.EMPTY);
		
		String containerName = null;
		if (selection instanceof ITreeSelection) {
			ITreeSelection ts = (ITreeSelection)selection;
			
			Object firstElement = ts.getFirstElement();
			if (firstElement != null) {
				if (firstElement instanceof File) {
					File f = (File)firstElement;
					containerName = f.getProject().getName() + "/src/asl";
				} else
				// is a Java Project
				if (firstElement instanceof IJavaProject) {
					IJavaProject pfr = (IJavaProject)firstElement;
					containerName = pfr.getProject().getName() + "/src/asl";
				}
				// is a Project
				else if (firstElement instanceof IProject) {
					IProject pfr = (IProject)firstElement;
					containerName = pfr.getName() + "/src/asl";
				}
				// is it an element of a Project
				else if (firstElement instanceof IJavaElement) {
					IJavaElement parent = (IJavaElement)firstElement;
					while (parent != null && !(parent instanceof IJavaProject)) {
						parent = parent.getParent();
					}
					if (parent != null && parent instanceof IJavaProject) {
						containerName = ((IJavaProject)parent).getProject().getName() + "/src/asl";
					}
				}
			}
		} else if (selection instanceof StructuredSelection) {
			Object fe = ((StructuredSelection)selection).getFirstElement();
			if (fe instanceof File) {
				File f = (File)fe;
				containerName = f.getProject().getName() + "/src/asl";
			} else {
				IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
				IProject[] projects = root.getProjects();
				for(int i = 0; i < projects.length; i++) {
					if (projects[i].isOpen() && projects[i].getFolder("src/asl").exists()) {
						containerName = projects[i].getName() + "/src/asl";
						break;
					}
				}
			}
		}
		
		if (containerName != null && !containerName.isEmpty()) {
			try {
				setContainerFullPath(ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(containerName)).getFullPath());
			} catch (Exception e) {
			}
		}
		
		setTitle("New AgentSpeak File");
		setDescription("This wizard creates a new AgentSpeak file.");
		this.setFileName("new_agent" + MAS2JHandler.AS_EXT);
	}
	
	public String getContainerName() {
		return this.getContainerFullPath().toString();
	}
}
